%%%-------------------------------------------------------------------
%%% @author flowerpig
%%% @copyright (C) 2020, <COMPANY>
%%% @doc
%%% @end
%%%-------------------------------------------------------------------
-module(scheduling).

-behaviour(gen_server).

-export([start_link/0, licensing/1, results/1, computing/2,
  player_into_room/1, player_out_room/1, room_number/1,delete_room_number/1]).

-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
  code_change/3]).

-include("../include/logger.hrl").
-include("../include/game.hrl").
-include("../include/myshare.hrl").

-define(SERVER, ?MODULE).

-record(scheduling_state, {data}).

%%%===================================================================
%%% Spawning and gen_server implementation
%%%===================================================================

start_link() ->
  gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).

init([]) ->
  ets:new(?ETS_MING_PAI_SCHEDULE, [set, protected, named_table, {keypos, #ming_pai_player.account}]),
  ets:new(?ETS_MING_PAI_ROOM_NUMBER, [set, protected, named_table, {keypos, #ming_pai_room_number.room_id}]),
  {ok, #scheduling_state{data = #{}}}.

%%玩家进入房间
handle_call({player_into_room, Room_id}, _From, State) ->
  case ets:lookup(?ETS_MING_PAI_ROOM_NUMBER, Room_id) of
    [] -> ets:insert(?ETS_MING_PAI_ROOM_NUMBER, #ming_pai_room_number{room_id = Room_id,number = 1});
    [Room_number] ->
      ets:update_element(?ETS_MING_PAI_ROOM_NUMBER, Room_id,
        {#ming_pai_room_number.number, Room_number#ming_pai_room_number.number + 1})
  end,
  {reply, ok, State};

%%玩家离开房间
handle_call({player_out_room, Room_id}, _From, State) ->
  case ets:lookup(?ETS_MING_PAI_ROOM_NUMBER, Room_id) of
    [] -> ok;
    [Room_number] ->
      case Room_number#ming_pai_room_number.number of
        0 -> ets:delete(?ETS_MING_PAI_ROOM_NUMBER, Room_id);
        _ -> ets:update_element(?ETS_MING_PAI_ROOM_NUMBER, Room_id,
          {#ming_pai_room_number.number, Room_number#ming_pai_room_number.number - 1})
      end
  end,
  {reply, ok, State};

%%删除过期的房间表
handle_call({delete_room_number, Room_id}, _From, State) ->
  ets:delete(?ETS_MING_PAI_ROOM_NUMBER, Room_id),
  {reply, ok, State};

%%房间人数
handle_call({room_number, Room_id}, _From, State) ->
  Num =
    case ets:lookup(?ETS_MING_PAI_ROOM_NUMBER, Room_id) of
      [] -> 0;
      [Room_number] ->
        Room_number#ming_pai_room_number.number
    end,
  {reply, Num, State};

handle_call({start_rob, Room_id, _Account}, _From, State) ->
  Ming_pai_players = ets:select(?ETS_MING_PAI_SCHEDULE, [{#ming_pai_player{room_id = Room_id, _ = '_'}, [], ['$_']}]),
  ?DEBUG("Ming_pai_players :~p", [Ming_pai_players]),
  Player_niu_num =
    lists:foldl(fun(#ming_pai_player{account = Account2, match = Match}, Acc) ->
      {_, Pai_val} = game_util:get_mingpai_bet_num(Match),
      Niu_num = game_util:niu_number(Pai_val),
      maps:put(Account2, Niu_num, Acc)
                end, #{}, Ming_pai_players),
  ?DEBUG("start rob player niu num :~p", [Player_niu_num]),
  {reply, ok, State};

handle_call({add_player, Player}, _From, State) ->
  ets:insert_new(?ETS_MING_PAI_SCHEDULE, [Player]),
  {reply, ok, State};

handle_call(_Request, _From, State = #scheduling_state{}) ->
  {reply, ok, State}.

handle_cast({del_player, Account}, State) ->
  ets:delete(?ETS_MING_PAI_SCHEDULE, Account),
  {noreply, State};

handle_cast(_Request, State = #scheduling_state{}) ->
  {noreply, State}.

handle_info(_Info, State = #scheduling_state{}) ->
  {noreply, State}.

terminate(_Reason, _State = #scheduling_state{}) ->
  ok.

code_change(_OldVsn, State = #scheduling_state{}, _Extra) ->
  {ok, State}.

%%%===================================================================
%%% Internal functions
%%%===================================================================
%%发牌添加玩家操作
licensing(Player = #ming_pai_player{}) ->
  gen_server:call(?MODULE, {add_player, Player}).

%%牌局结果删除玩家
results(Account) ->
  gen_server:cast(?MODULE, {del_player, Account}).

%%抢庄倍数计算
computing(Room_id, Account) ->
  gen_server:call(?MODULE, {start_rob, Room_id, Account}).

player_into_room(Room_id) ->
  gen_server:call(?MODULE, {player_into_room, Room_id}).

player_out_room(Room_id) ->
  gen_server:call(?MODULE, {player_out_room, Room_id}).

room_number(Room_id) ->
  gen_server:call(?MODULE, {room_number, Room_id}).

delete_room_number(Room_id)->
  gen_server:call(?MODULE, {delete_room_number, Room_id}).
